package com.mobilnaElka.activities.calendar;

import android.app.*;
import android.app.AlertDialog.Builder;
import android.content.*;
import android.os.Bundle;
import android.view.*;
import android.webkit.*;

import com.mobilnaElka.AppConsts;
import com.mobilnaElka.activities.R;
import com.mobilnaElka.models.calendar.Consts;

public class TimetableTabActivity extends Activity {
	private static final int MENU_SETTINGS = 0;
//	private static final int MENU_INFO = 1;
	private WebView webview;
	private SharedPreferences preferences;
	private Builder builder;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		preferences = getSharedPreferences(AppConsts.SETTINGS_PREF,
				Context.MODE_PRIVATE);

		setContentView(R.layout.timetable_layout);
		webview = (WebView) findViewById(R.id.timetableWebView);

		String textFromPreferences = preferences.getString(
				Consts.SETTINGS_FILE_NAME, "");
		if (textFromPreferences != null && !textFromPreferences.equals("")) {
			webview.loadUrl("file:///" + textFromPreferences);
		} else {
			webview.loadDataWithBaseURL("", getString(R.string.def_timetable),
					"text/html", "utf-8", "");
		}

		webview.setWebViewClient(new WebViewClient() {
			@Override
			public void onPageFinished(WebView webView, String url) {
				String textFromPreferences = preferences.getString(
						Consts.SETTINGS_FILE_NAME, null);
				super.onPageFinished(webView, url);
				if (textFromPreferences != null)
					webView.setInitialScale(countScale(
							findViewById(R.id.timetableWebView).getHeight(),
							findViewById(R.id.timetableWebView).getWidth()));
			}
		});

		WebSettings settings = webview.getSettings();
		settings.setBuiltInZoomControls(true);
		settings.setUseWideViewPort(true);
		settings.setTextSize(WebSettings.TextSize.LARGER);

		
		builder = new AlertDialog.Builder(this);
	}

	@Override
	public void onResume() {
		super.onResume();
		SharedPreferences preferences = getSharedPreferences(
				AppConsts.SETTINGS_PREF, Context.MODE_PRIVATE);
		String textFromPreferences = preferences.getString(
				Consts.SETTINGS_FILE_NAME, "");
		if (textFromPreferences != null && !textFromPreferences.equals("")) {
			webview.loadUrl("file:///" + textFromPreferences);
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_SETTINGS, 0, R.string.Settings);
//		menu.add(0, MENU_INFO, 0, R.string.Info);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_SETTINGS:
			Intent intentSettings = new Intent(this,
					CalendarSettingsActivity.class);
			finish();
			startActivityForResult(intentSettings, 0);
			return true;
//		case MENU_INFO:
//			return true;
		}
		return false;
	}
	
	@Override
	public void onBackPressed() {
		builder.setMessage(getString(R.string.are_you_sure_to_close))
		.setPositiveButton(getString(R.string.yes),
				dialogClickListener)
		.setNegativeButton(getString(R.string.no),
				dialogClickListener).show();

	}
	
	DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			switch (which) {
			case DialogInterface.BUTTON_POSITIVE:
				// Yes button clicked
				finish();
				System.runFinalization();
				break;

			case DialogInterface.BUTTON_NEGATIVE:
				// No button clicked
				break;
			}
		}
	};

	private int countScale(int webViewHeight, int webViewWidth) {
		if (webViewHeight == 0 || webViewWidth == 0)
			return 1;

		Display display = getWindowManager().getDefaultDisplay();
		int width = display.getWidth();
		int height = display.getHeight();

		int scaleWidth = width / webViewWidth;
		int scaleHeight = height / webViewHeight;

		if (scaleWidth < scaleHeight)
			return scaleWidth;
		return scaleHeight;

	}

}